<?php
/**
 * Created by PhpStorm.
 * User: wei
 * Date: 2016/2/23
 * Time: 16:24
 */
namespace Api\Controller;
use Think\Controller;
class AddressController extends Controller
{
    //添加收货地址
    /**
     * type:1维修，2装修，3承包商
     */
    public function addAddress()
    {
        //解决跨域
        echoHeader();
        $contacts = I('post.contacts', '');
        $user_id = I('post.user_id', '');
        $city = I('post.city', '');
        $province = I('post.province', '');
        $gender = I('post.gender', '');
        $doorplate = I('post.doorplate', '');    //门牌号
        $address = I('post.address', '');
        $phone = I('post.phone', '');
        $type= I('post.type', '');     //1,维修。2.装修。3,商城
        $lng=I('post.lng');
        $lat=I('post.lat');
        $status = 0;
        $add_time = time();
        $key= I('post.key', '');
        $where['user_id']=$user_id;
        $user=M('user')->where($where)->limit(1)->select();
        $isLogin=verifyKey($user[0]['phone'],$key);
        /*if(!$isLogin){
            $data=array(err_code=>1003,err_msg=>'用户状态出错');
            $this->ajaxReturn($data,'JSON');
        }*/

        if (!$contacts) {
            $data = array(err_code => 1001, err_msg => '联系人不能为空');
            $this->ajaxReturn($data, 'JSON');
        }

        if (!$gender) {
            $data = array(err_code => 1001, err_msg => '性别不能为空');
            $this->ajaxReturn($data, 'JSON');
        }

        if (!$address) {
            $data = array(err_code => 1001, err_msg => '地址不能为空');
            $this->ajaxReturn($data, 'JSON');
        }

        if (!$phone) {
            $data = array(err_code => 1001, err_msg => '手机号码不能为空');
            $this->ajaxReturn($data, 'JSON');
        }

        if($type<1 || $type>3){
            $data = array(err_code => 1201, err_msg => 'type出错');
            $this->ajaxReturn($data, 'JSON');
        }

        if(empty($lat) || empty($lng)){
            $data = array(err_code => 1101, err_msg => '无法定位地址，请重新填写');
            $this->ajaxReturn($data, 'JSON');
        }

        if (!preg_match("/^1[3-9][0-9]{1}[0-9]{8}$|15[0189]{1}[0-9]{8}$|189[0-9]{8}$/", $phone)) {
            //验证通过
            $data = array(err_code => 403, err_msg => '请正确输入手机号码');
            $this->ajaxReturn($data, 'JSON');
        }
        $addressObj = M('address');
        $add['contacts'] = $contacts;
        $add['address'] = $address;
        $add['gender'] = $gender;
        $add['city'] = $city;
        $add['province'] = $province;
        $add['phone'] = $phone;
        $add['status'] = $status;
        $add['add_time'] = $add_time;
        $add['user_id'] = $user_id;
        $add['doorplate'] = $doorplate;
        $add['type'] = $type;
        $add['lng']=$lng;
        $add['lat']=$lat;
      //  $param="ak=".C('MAP_CONFIG')['ak']."&output=json&address=$address&city=$city";
     /*   $param=array(
            'ak'=>C('MAP_CONFIG')['ak'],
            'output'=>'json',
            'address'=>$address,
            'city'=>$city
        );
        $response=http(C('MAP_CONFIG')['url'],$param);
        $response=json_decode($response);
        if($response->status==0){
            $add['lng']=$response->result->location->lng;
            $add['lat']=$response->result->location->lat;
        }else{
            $data = array(err_code => 1101, err_msg => '无法定位地址，请重新填写');
            $this->ajaxReturn($data, 'JSON');
        }*/
        $saveData = $addressObj->add($add);
        if ($saveData) {
            $data = array(err_code => 200, err_msg => '保存成功');
            $this->ajaxReturn($data, 'JSON');
        } else {
            $data = array(err_code => 403, err_msg => '保存失败');
            $this->ajaxReturn($data, 'JSON');
        }
    }

    //删除收货地址
    public function delAddress(){
        //解决跨域
        echoHeader();
        $user_id = I('post.user_id', '');
        $address_id=I('post.address_id', '');
        $key= I('post.key', '');
        $where['user_id']=$user_id;
        $user=M('user')->where($where)->limit(1)->select();
        $isLogin=verifyKey($user[0]['phone'],$key);
       /* if(!$isLogin){
            $data=array(err_code=>403,err_msg=>'用户状态出错');
            $this->ajaxReturn($data,'JSON');
        }*/

        $address=M('address');
        $data['is_del']=1;
        if($address->where("user_id='$user_id' and id=$address_id")->save($data)===false){
            $data=array(err_code=>403,err_msg=>'删除失败');
            $this->ajaxReturn($data,'JSON');
        }else{
            $data=array(err_code=>200,err_msg=>'删除成功');
            $this->ajaxReturn($data,'JSON');
        }

    }

    //用户收货地址列表
    public function address(){
        //解决跨域
        echoHeader();
        $user_id = I('post.user_id', '');
        $key= I('post.key', '');
        $type= I('post.type', 1);
        $where['user_id']=$user_id;
        $user=M('user')->where($where)->limit(1)->select();

        //检查用户是否已登录
        $isLogin=verifyKey($user[0]['phone'],$key);
       /* if(!$isLogin){
            $data=array(err_code=>403,err_msg=>'用户状态出错');
            $this->ajaxReturn($data,'JSON');
        }*/
        $address=M('address');
        $map['is_del']=0;
        if($type==3){
            $map['type']=3;
        }else{
            $map['type']=array('in','1,2');
        }

        $map['user_id']=$user_id;
        $list=$address->where($map)->order(array('status'=>'desc'))->select();


        $data=array(err_code=>200,err_msg=>$list);
        $this->ajaxReturn($data,'JSON');
    }






    //用户更改收货地址
    public function updateAddress(){
        //解决跨域
        echoHeader();
        $contacts = I('post.contacts', '');
        $user_id = I('post.user_id', '');
        $key = I('post.key', '');
        $gender = I('post.gender', '');
        $city = I('post.city', '');               //城市
        $province = I('post.province', '');     //省份
        $address = I('post.address', '');
        $doorplate = I('post.doorplate', '');
        $phone = I('post.phone', '');
        $city = I('post.city', '');
        $address_id = I('post.address_id', '');
        $where['user_id']=$user_id;
        $user=M('user')->where($where)->limit(1)->select();

        if (!preg_match("/^1[3-9][0-9]{1}[0-9]{8}$|15[0189]{1}[0-9]{8}$|189[0-9]{8}$/", $phone)) {
            //验证通过
            $data = array(err_code => 1301, err_msg => '请正确输入手机号码');
            $this->ajaxReturn($data, 'JSON');
        }

        //检查用户是否已登录
        $isLogin=verifyKey($user[0]['phone'],$key);
      /*  if(!$isLogin){
            $data=array(err_code=>403,err_msg=>'用户状态出错');
            $this->ajaxReturn($data,'JSON');
        }*/


        $data=array();
        $param=array(
            'ak'=>C('MAP_CONFIG')['ak'],
            'output'=>'json',
            'address'=>$address,
            'city'=>$city
        );
        $response=http(C('MAP_CONFIG')['url'],$param);
        $response=json_decode($response);
        if($response->status==0){
            $data['lng']=$response->result->location->lng;
            $data['lat']=$response->result->location->lat;
        }

        empty($contacts)?'':$data['contacts']=$contacts;
        empty($gender)?'':$data['gender']=$gender;
        empty($address)?'':$data['address']=$address;
        empty($phone)?'':$data['phone']=$phone;
        empty($city)?'':$data['city']=$city;
        empty($province)?'':$data['province']=$province;
        empty($doorplate)?'':$data['doorplate']=$doorplate;



        $save=M('address')->where("id=$address_id")->save($data);

        if($save===false){
            $data=array(err_code=>403,err_msg=>'修改失败');
            $this->ajaxReturn($data,'JSON');
        }else{
            $data=array(err_code=>200,err_msg=>'修改成功');
            $this->ajaxReturn($data,'JSON');
        }

    }




    //设置默认地址
    public function defaultAddress(){
        //解决跨域
        echoHeader();
        $address_id=I('post.address_id','','int');
        $user_id=I('post.user_id','','int');
        if(empty($address_id)){
            $data=array(err_code=>1001,err_msg=>'地址不能为空');
            $this->ajaxReturn($data,'JSON');
        }

        $updateWhere['status']=1;
        $updateWhere['user_id']=$user_id;
        $updatemap['status']=0;
        M('address')->where($updateWhere)->save($updatemap);

        //更新选中地址为默认地址
        $where['id']=$address_id;
        $where['user_id']=$user_id;
        $map['status']=1;
        if(M('address')->where($where)->save($map)==false){
            $data=array(err_code=>1401,err_msg=>'地址不存在');
            $this->ajaxReturn($data,'JSON');
        }

        $data=array(err_code=>200,err_msg=>'修改成功');
        $this->ajaxReturn($data,'JSON');
    }






    //地址详情
    public function addressDetail(){
        //解决跨域
        echoHeader();
        $address_id=I('post.address_id');
        $user_id=I('post.user_id');
        $phone=I('post.phone');
        $key=I('post.key');

        $verify=verifyKey($phone,$key);
        /*if(!$verify){
            $data=array(err_code=>403,err_msg=>'登录超时');
            $this->ajaxReturn($data,'JSON');
        }*/
        $where['id']=$address_id;
        $where['user_id']=$user_id;
        $where['is_del']=0;
        $detail=M('address')->where($where)->select();
        if(empty($detail)){
            $data=array(err_code=>1301,err_msg=>'地址已被删除');
            $this->ajaxReturn($data,'JSON');
        }

        $data=array(err_code=>200,err_msg=>$detail);
        $this->ajaxReturn($data,'JSON');

    }


    public function test(){
        var_dump(NOW_HOST);
    }
}